#include "abstractcard.h"
#include <QDataStream>

#include <QDebug>

//AbstractCard::AbstractCard(  /* Color */          ManaDefinitionType newCardColor,
//                             /* Name */           QString newCardName,
//                             /* Description */    QString newCardDescription) :
//    QObject(0)
//{
//    m_CardName = newCardName;
//    m_CardDescription = newCardDescription;
//    AddCardColor(newCardColor);
//}

AbstractCard::AbstractCard(QDomDocument *xmlDefinition):
        QObject(0)
{
    m_CharacteristicsList << WORD_TYPE << WORD_NAME << WORD_DESCRIPTION << WORD_SUBTYPE;

    m_XmlDefinition = xmlDefinition->toString(3);

    convertFromXML(xmlDefinition, m_CharacteristicsList);
}

AbstractCard::AbstractCard(QString xmlDefinition):
        QObject(0)
{

    m_CharacteristicsList << WORD_TYPE << WORD_NAME << WORD_DESCRIPTION << WORD_SUBTYPE;
    m_XmlDefinition = xmlDefinition;

    QDomDocument *doca = new QDomDocument();
    doca->setContent(xmlDefinition);
    convertFromXML(doca, m_CharacteristicsList);
}

void AbstractCard::convertFromXML(QDomDocument *xmlDefinition, QVector<QString> propList)
{
    QDomElement rootElement = xmlDefinition->documentElement();

    foreach( QString propName, propList)
    {

        QDomNodeList nodes = rootElement.elementsByTagName(propName);
        if (nodes.isEmpty())
            continue;

        this->setProperty(QString("Card" + propName).toAscii(), nodes.item(0).toElement().attribute(WORD_ATTR_VALUE));
    }

    QDomElement element = rootElement.firstChildElement();
    while (!element.isNull())
    {
        if (element.attributes().isEmpty()
            && element.childNodes().isEmpty()
            && (m_Properties.indexOf(element.tagName()) == -1))
        {

            m_Properties.append(element.tagName());
        }

        element = element.nextSiblingElement();
    }

}

QVector<ManaDefinitionType> AbstractCard::GetCardColor()
{
    return m_CardColor;
}

void AbstractCard::SetCardColor(QVector<ManaDefinitionType> newColor)
{
    m_CardColor = newColor;
}

void AbstractCard::AddCardColor(ManaDefinitionType newColor)
{
    m_CardColor << newColor;
}


QString AbstractCard::toXmlDefinition()
{
    if (!m_XmlDefinition.isNull())
        return m_XmlDefinition;
    else
        return "";
}

QString AbstractCard::getCardName()
{
    return m_CardName;
}

void AbstractCard::setCardName(QString value)
{
    m_CardName = value;
}

QString AbstractCard::getCardDescription()
{
    return m_CardDescription;
}

void AbstractCard::setCardDescription(QString value)
{
    m_CardDescription = value;
}

QString AbstractCard::getCardType()
{
    return m_CardType;
}

void AbstractCard::setCardType(QString value)
{
    m_CardType = value;
}

QString AbstractCard::getCardSubType()
{
    return m_CardSubType;
}

void AbstractCard::setCardSubType(QString value)
{
    m_CardSubType = value;
}

AbstractCard::CardState AbstractCard::getCardState()
{
    return m_CardState;
}

void AbstractCard::setCardState(AbstractCard::CardState state)
{
    m_CardState = state;
}

